﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using eCitizen.DataContracts;
using eCitizen.eAlerts.Utilities;

namespace eCitizen.eAlerts.BusinessService
{

    public class MessageBroadcastManager : ServiceBase
    {
        private enum MessageType
        {
            SmsMessage,
            EmailMessage
        }

        public void BroadcasteAlertMessages(List<Subscriber> subscriberList)
        {
            SmsManager smsMessageSender = new SmsManager();
            SmtpManager emailMessageSender = new SmtpManager();
            string smsFileName = string.Empty;
            DateTime processDateTime = DateTime.Now;

            // 1. Query into the list of the subscribers to get Sms/Email Messages to send out
            SmsMessageList smsMessageList = CreateSmsMessagesForSubscribers(subscriberList);
            EmailMessageList emailMessageList = CreateEmailMessagesforSubscribers(subscriberList);

            int runningFileNo = CurrentServiceHelper.GetRunningFileNo(ServiceType.SendAlertService, processDateTime);
            runningFileNo += 1;

            smsFileName = smsMessageSender.Broadcast(CurrentConfiguration, smsMessageList, runningFileNo);
            emailMessageSender.Broadcast(CurrentConfiguration, emailMessageList);

            CurrentServiceHelper.LogeAlertFileToProcess(smsFileName, false, processDateTime);

        }

        private SmsMessageList CreateSmsMessagesForSubscribers(List<Subscriber> subscriberList)
        {
            SmsMessageList messageList = new SmsMessageList();
            List<Subscriber> filteredList = new List<Subscriber>();

            // 1. Filter out the subscription 
            filteredList = GetSubscriberbyType(subscriberList, MessageType.SmsMessage);

            // 2. Populate the required data into the Sms Messages
            foreach (Subscriber subscriberInfo in filteredList)
            {
                SmsMessage message = new SmsMessage();
                message.AgencyId = subscriberInfo.SubscriptionAgencyCode;
                message.HandPhone = subscriberInfo.HandPhone;
                message.MessageContent = ConstructMessageContent(subscriberInfo);

                messageList.SmsMessages.Add(message);
            }

            return messageList;
        }

        private EmailMessageList CreateEmailMessagesforSubscribers(List<Subscriber> subscriberList)
        {
            EmailMessageList messageList = new EmailMessageList();
            List<Subscriber> filteredList = new List<Subscriber>();

            // 1. Filter out the subscription 
            filteredList = GetSubscriberbyType(subscriberList, MessageType.EmailMessage);

            // 2. Populate the required data into the Sms Messages
            foreach (Subscriber subscriberInfo in filteredList)
            {
                EmailMessage message = new EmailMessage();
                message.EmailAddress = subscriberInfo.Email;
                message.MessageContent = ConstructMessageContent(subscriberInfo);

                messageList.EmailMessages.Add(message);
            }
            return messageList;
        }

        private List<Subscriber> GetSubscriberbyType(List<Subscriber> subscriberList, MessageType messageType)
        {
            List<Subscriber> filteredList = new List<Subscriber>();

            if (messageType == MessageType.EmailMessage)
            {
                filteredList = subscriberList.Where(c => c.EmailSubscription == true).ToList();
            }
            else
            {
                filteredList = subscriberList.Where(c => c.SmsSubscription == true).ToList();
            }

            return filteredList;
        }

        private string ConstructMessageContent(Subscriber subscriberInfo)
        {
            string messageContent;

            messageContent = subscriberInfo.MessageDescription;
            if (subscriberInfo.AmountDue.HasValue) messageContent += String.Format("Amount Dued: {0}", subscriberInfo.AmountDue.Value.ToString());

            return messageContent;
        }
    }
}
